from rest_framework.views import APIView
from rest_framework.permissions import IsAdminUser, IsAuthenticated
from users.models import User
from meiduo_admin.serializers.users import UserSerializer
from rest_framework.response import Response
from rest_framework import generics
from meiduo_admin.utils.pagination import MeiduoPagination

# class UsersView(APIView):
#     permission_classes = [IsAdminUser]
#
#     def get(self,request):
#         request.query_params.get()
#         queryset = User.objects.filter(is_staff=False).order_by('-id')
#         serializer = UserSerializer(queryset,many=True)
#
#         return Response(serializer.data)
#
#     def post(self,requset):
#         param_dict = requset.data
#
#         serializer = UserSerializer(data=param_dict)
#         serializer.is_valid(raise_exception=True)
#
#         instance = serializer.save()
#
#         return Response(serializer.data)


class UsersView(generics.ListCreateAPIView):
    # permission_classes = [IsAdminUser]
    # 说明：需要通过试图对象获取请求对象，在获取参数
    # 使用queryset属性时，这是个类属性，无法访问实例属性
    # 使用get_queryset方法时，这是个实例方法，可以访实例属性
    # queryset = User.objects.filter(is_staff=False).order_by('-id')
    def get_queryset(self):
        # self.request,self.args,self.kwargs
        queryset = User.objects.filter(is_staff=False)
        # 查询用户名条件
        keyword = self.request.query_params.get('keyword')
        if keyword:
            queryset = queryset.filter(username__contains=keyword)
        # 排序：最新的放在最前面
        queryset = queryset.order_by('-id')
        # 返回查询集
        return queryset
        # 查询语句是惰性执行，这里只是在拼接查询语句，需要数据在做查询

    serializer_class = UserSerializer
    # 分页
    pagination_class = MeiduoPagination
